package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketAftersale;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.aftersale.AfterSaleVo;
import com.cskaoyan.service.wx.AfterSaleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author by yxy_8175@163.com
 * @Description TODO
 * @Date 2022-11-24 11:02
 */

@RestController
@RequestMapping("wx/aftersale")
public class WxAfterSaleController {

    @Autowired
    AfterSaleService afterSaleService;

    /*
     * @Description TODO 获取当前用户的售后列表
     * @param
     * @return
     * @date 2022-11-24 13:10
     * @auther yxy_8175@163.com
    */
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo,Short status){
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        if (principal == null){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId = principal.getId();

        CommonData<AfterSaleVo> data =  afterSaleService.queryAllAfterSale(pageInfo,status,userId);

        return BaseRespVo.ok(data);
    }


    /*
     * @Description TODO 申请售后，将申请信息存入数据库
     * @param
     * @return
     * @date 2022-11-24 13:12
     * @auther yxy_8175@163.com
    */
    @PostMapping("submit")
    public BaseRespVo  submit(MarketAftersale aftersale){
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        if (principal == null){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId = principal.getId();

        aftersale.setUserId(userId);
        afterSaleService.insertAfterSale(aftersale);
        if (aftersale.getId() == null){
            return BaseRespVo.error(401,"申请失败");
        }
        return BaseRespVo.ok();
    }

}
